package br.com.searchpeople.activity;

import br.com.searchpeople.service.SearchPeopleService;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends Activity{
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		SharedPreferences settings = getSharedPreferences("general", Context.MODE_PRIVATE);
		String login = settings.getString("login", "");
		if (!isMyServiceRunning()) {
			Intent intent = new Intent(this, SearchPeopleService.class);
			startService(intent);
		}
		if (!login.equals("")) {
			startActivity(new Intent(getApplicationContext(), TimelineActivity.class));
		} else {
			startActivity(new Intent(getApplicationContext(), LoginActivity.class));
		}
		
		
	}
	
	@Override
	protected void onStop() {
		super.onStop();
		this.finish();
	}
	
	private boolean isMyServiceRunning() {
	    ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
	    for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
	        if (SearchPeopleService.class.getName().equals(service.service.getClassName())) {
	            return true;
	        }
	    }
	    return false;
	}
	
}
